package com.truyentranh.modules.truyentranh;

import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Hashtable;

import com.androidquery.callback.AjaxStatus;
import com.androidquery.callback.BitmapAjaxCallback;
import com.truyentranh.R;
import com.truyentranh.controls.WarperImageView;
import com.truyentranh.data.TamDiemDetailModuleData;
import com.truyentranh.data.TamDiemMetalData;
import com.truyentranh.utils.AppSettingParams;
import com.truyentranh.utils.GlobalData;
import com.truyentranh.utils.IImageCached;
import com.truyentranh.utils.Logger;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class TruyenTranhChapterRenderAdapter extends ArrayAdapter<TruyenTranhChapterData> {

	private ArrayList<TruyenTranhChapterData> blockList = null;
	private Hashtable<String, String> bookmark;
	public TruyenTranhChapterRenderAdapter(Context context, int resource, int textViewResourceId, ArrayList<TruyenTranhChapterData> objects) {
		super(context, resource, textViewResourceId, objects);
		this.setBlockList(objects);
		
//		bookmark = TruyenTranhSession.getAllBookMark(context);
	}
	
	public void setBookMark(Hashtable<String, String> bookmark){
		this.bookmark = bookmark;
	}
	
	public void setNewData(ArrayList<TruyenTranhChapterData> blockList){
		this.setBlockList(blockList);
		notifyDataSetChanged();
	}
	
	public void clearData(){
		if(blockList != null){
			blockList.clear();
		}
	}
	
	public void appendData(ArrayList<TruyenTranhChapterData> inputNewData){
		if(getBlockList() != null && inputNewData != null){
			final int size = inputNewData.size();
			for(int i = 0 ; i < size ; i++){
				getBlockList().add(inputNewData.get(i));
			}
			notifyDataSetChanged();
		}
	}
	

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub

		LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View rowHeader = inflater.inflate(R.layout.truyentranh_render_chapterlist, parent, false);
		rowHeader.setPadding(2,2,2,2);
		TextView header = (TextView) rowHeader.findViewById(R.id.nameView);
		
		ImageView imageViewBookMark = (ImageView) rowHeader.findViewById(R.id.bookmarkIcon);
		final WarperImageView imageView = (WarperImageView) rowHeader.findViewById(R.id.avatarView);
		
		
		TruyenTranhChapterData module = this.getBlockList().get(position);
		if(bookmark != null && bookmark.containsKey(module.id)){
			imageViewBookMark.setVisibility(View.VISIBLE);
		} else {
			imageViewBookMark.setVisibility(View.GONE);
		}
		
		
		header.setText(module.getName());
		
		String url = module.getAvatar();
		GlobalData.mAQ.id(imageView).image(url, GlobalData.opt_avatar);
		
//		GlobalData.mAQ.id(imageView).image(decode, true, true, 400, 0,
//                new BitmapAjaxCallback() {
//            @Override
//            public void callback(String url,
//                    ImageView imageview, Bitmap bm,
//                    AjaxStatus status) 
//            {            	
//                if (bm != null) 
//                {
//                	if (imageView != null) {
//						imageView.setImageBitmap(bm);
//						imageView.setVisibility(View.VISIBLE);
//					}
//                } 
//            }
//        });
		
//		mImageLoader.DisplayImageView(false, decode, 0, 0, new IImageCached() {
//
//			public void onImageReturn(Activity act,final Bitmap bitmap) {
//				if (act != null) {
//					act.runOnUiThread(new Runnable() {
//
//						public void run() {
//							
//							if (imageView != null) {
//								imageView.setImageBitmap(bitmap);
//								imageView.setVisibility(View.VISIBLE);
//							}
//								
//						}
//					});
//				}
//			}
//		});
//		Bitmap bitmap = ImageManager.getInstance().getImage(decode, new IImageCached() {
//
//			public void onImageReturn(final Bitmap bitmap) {
//
//				if (bitmap != null && !bitmap.isRecycled()) {
//					if (ImageManager.currentActivity != null) {
//						ImageManager.currentActivity.runOnUiThread(new Runnable() {
//
//							public void run() {
//								if (bitmap != null && !bitmap.isRecycled())
//									imageView.setImageBitmap(bitmap);
//							}
//						});
//					}
//				}
//			}
//		}, true);
//
//		if (bitmap != null) {
//			imageView.setImageBitmap(bitmap);
//		}

		return rowHeader;
	}

	public ArrayList<TruyenTranhChapterData> getBlockList() {
		return blockList;
	}

	public void setBlockList(ArrayList<TruyenTranhChapterData> blockList) {
		this.blockList = blockList;
	}
}
